package com.ktkj.init.shiro;

import com.alibaba.fastjson.JSON;
import com.ktkj.init.response.Response;
import org.apache.shiro.web.filter.authc.UserFilter;

import javax.servlet.ServletRequest;
import javax.servlet.ServletResponse;
import java.io.IOException;

public class LoginFilter extends UserFilter {

    /**
     * 这个方法用于处理未登录时页面重定向的逻辑
     * 因此，只要进入了这个方法，就意味着登录失效了
     * 我们只需要在这个方法里，给前端返回一个登录失效的状态码即可
     *
     * @param request
     * @param response
     * @throws IOException
     */
    @Override
    protected void redirectToLogin(ServletRequest request, ServletResponse response) throws IOException {
        response.setContentType("application/json;charset=UTF-8");
        Response response1 = new Response(401, "请先登录！", false);
        response.getWriter().write(JSON.toJSONString(response1));
        //response.getWriter().write("{\"code\":401,\"message\":\"请先登录！\",\"body\":null,\"timestamp\":"+System.currentTimeMillis()+"}");
    }
}
